var wUtils = require('../../utils/wechatRequest.js');
var app = getApp();
Page({
  data: {
    name: '',
    qrCodeUrl: ''
  },
  onLoad: function () {

  },
  bindName: function (e) {
    this.setData({
      name: e.detail.value
    });
  },

  priviewimage: function () {
    wx.previewImage({
      urls: [this.data.qrCodeUrl],
    });
  },

  bindsubmit: function () {
    console.log('bind...start');
    // 页面需要美化, 需要个列表页
    console.log(this.data.name);

    var that = this;
    var postData = {};
    postData.name = this.data.name;

    wUtils.doGet('/app/user/qrcode/create?name=' + this.data.name, null, function (res) {
      if (res.code == 0) {
        console.log('申请成功.');
        // 需要重新设置user.js的userInfo属性, 绑定成功之后应该刷新积分
        that.setData({
          qrCodeUrl: 'data:image/png;base64,' + res.image
        });
        
      } else {
        console.log('申请失败');
        console.log('失败原因:' + res.msg);
      }
    });
    //let url = app.host + '/app/user/qrcode/create?name=' + this.data.name;

    //console.log(url);

    // wx.downloadFile({
    //   url: url,
    //   header: {"token": wx.getStorageSync('token')},
    //   success: function(res) {

    //     console.log('下载成功');
    //     var filePath = res.tempFilePath;
    //     console.log('filePath is ' + filePath);
    //     if (res.statusCode === 200) {
    //       console.log('statuscode == 200');
          // wx.openDocument({
          //   filePath: filePath,
          //   success: function(res) {
          //     console.log('打开成功');
          //     console.log(res);
          //   },
          //   fail: function(res) {
          //     console.log('fail');
          //     console.log(res);
          //   }
          // })
    //     }
    //   },
    //   fail: function() {
    //     console.log('下载失败.');
    //   }
    // })

    console.log('bind...end');
  }
})